Skip to content

fix(tracer): gate SDK behind env flag, handle errors gracefully, add shutdown hook - #59

Merged
priscaenoch merged 1 commit into
octraban:mainfrom
marvel6828:fix/tracer-env-gate
Aug 22, 2026
Merged

fix(tracer): gate SDK behind env flag, handle errors gracefully, add shutdown hook#59
priscaenoch merged 1 commit into
octraban:mainfrom
marvel6828:fix/tracer-env-gate

Conversation

@marvel6828

@marvel6828 marvel6828 commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Problem

src/tracer.ts called sdk.start() unconditionally at import time with OTLP_ENDPOINT defaulting to http://localhost:4318. Every run of the service started tracing and attempted to export spans to a local collector , even in development or CI where no collector exists , producing connection errors, overhead, and no way to disable it. There was also no SIGINT handler and no graceful shutdown.

Changes

src/tracer.ts

  • isTracingEnabled(env) :pure gate function; returns true only when TRACING_ENABLED=true/1 or OTLP_ENDPOINT is set (non-empty). Off by default.
  • initTracing(env) : wraps NodeSDK construction and sdk.start() in a try/catch; any startup error is logged as a warn and the process continues. Has a duplicate-call guard so it's safe to call multiple times.
  • Auto-calls initTracing() at import time : no change to existing callers.
  • Registers both SIGTERM and SIGINT shutdown hooks (SIGINT was missing before).
  • Exports isTracingEnabled and initTracing for direct unit testing.

tests/tracer.test.ts (new, 22 tests)

  • isTracingEnabled(): all env combinations , absent, unrecognised, true, 1, endpoint-only, empty endpoint
  • initTracing(): disabled path (NodeSDK never called), enabled path (SDK starts), error handling (no throw on start() failure, warning logged)
  • withSpan(): success path, ERROR status set on throw, span always ends in finally

package.json

  • Added tests/tracer.test.ts to the npm test command so it runs in CI.

.env.example

  • Added commented-out TRACING_ENABLED, OTLP_ENDPOINT, SERVICE_NAME.

README.md

  • Added OpenTelemetry Tracing section with env var table and Jaeger quick-start example.

Acceptance criteria

  • Tracing is off by default ,only starts when TRACING_ENABLED=true/1 or OTLP_ENDPOINT is set
  • A missing/unreachable collector does not produce crashes or error spam
  • Spans flush cleanly on SIGTERM and SIGINT
  • TRACING_ENABLED, OTLP_ENDPOINT, SERVICE_NAME documented in README and .env.example
  • All CI checks pass (npm run build ✅, npm run lint 0 errors ✅, npm test 209/209 ✅)

##Closes
closes #43

…shutdown hook

- Add isTracingEnabled() that checks TRACING_ENABLED=true/1 or any
  non-empty OTLP_ENDPOINT; tracing is off by default
- Wrap sdk.start() in try/catch: connection/config errors are logged
  as warnings and never crash or spam the process
- Register SIGTERM and SIGINT handlers that call sdk.shutdown() so
  spans flush cleanly on process exit
- Expose initTracing(env) for testability; auto-called at import time
- Export isTracingEnabled() for direct unit testing of gate logic
- Add 22 tests covering: disabled default, all enabled paths (true/1/
  OTLP_ENDPOINT), error handling (no throw, warn logged), withSpan
  lifecycle (success, error status, span always ends)
- Document TRACING_ENABLED, OTLP_ENDPOINT, SERVICE_NAME in README and
  .env.example; add Jaeger quick-start example
- Wire tests/tracer.test.ts into the npm test command

Fixes: tracing started unconditionally at import; SIGINT not handled;
       no env gate; connection errors could spam the process

Closes #<tracer-env-gate-issue>
@priscaenoch
priscaenoch merged commit 894e54b into octraban:main Aug 22, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make OpenTelemetry tracing opt-in and handle exporter failures

2 participants